<template>
    <div class="qlist" v-scroll="getProduct">
        <div class="container">
            <div class="boxTable">
                <el-table :data="phoneList" border  >
                    <el-table-column label="顺序" type="index" align="center" width="50"> </el-table-column>
                    <el-table-column label="国际条码" align="center" prop="barCode" width="125"></el-table-column>
                    <el-table-column label="商品图片" align="center" width="90">
                        <template slot-scope="{ row }">
                            <a :href="'/#/detail/' + row.id">
                                <img style="width: 60px; height: 60px" v-lazy="$img + row.pic" />
                            </a>
                        </template>
                    </el-table-column>
                    <el-table-column label="商品编号" prop="productSn" width="80"></el-table-column>
                    <el-table-column label="商品颜色" prop="color" width="80"></el-table-column>
                    <el-table-column label="外包装数量" prop="fclQuantity" width="100"></el-table-column>
                    <el-table-column label="内包装数量" prop="innerPackageQuantity" width="100"></el-table-column>
                    <el-table-column label="商品名称" align="center" prop="name"></el-table-column>
                    <el-table-column label="库存数量" prop="stock" width="80"></el-table-column>
                    <el-table-column label="您的价格" prop="price" width="80"></el-table-column>
                    <el-table-column label="订购数量" align="center" width="160">
                        <template slot-scope="scope">
                            <el-input-number
                                size="mini"
                                @change="(val) => numChange(val, scope.$index, scope.row.price)"
                                v-model="scope.row.num"
                                :min="0"
                                :max="scope.row.stock"
                            ></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column label="小计" center prop="total"> </el-table-column>
                </el-table>
            </div>
        </div>
    </div>
</template>
<script>
import ServiceBar from './../components/ServiceBar';
import Modal from './../components/Modal';
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
export default {
    name: 'index',
    components: {
        swiper,
        swiperSlide,
        ServiceBar,
        Modal
    },
    data() {
        return {
            phoneList: [],
            pageNum: 1,
            showModal: false
        };
    },
    mounted() {
        this.init();
    },
    directives: {
        scroll: {
            bind: function (el, binding) {
                window.addEventListener('scroll', function () {
                    // console.log(document.documentElement.scrollTop,document.documentElement.clientHeight, document.documentElement.scrollHeight)
                    if (
                        document.documentElement.scrollTop + document.documentElement.clientHeight >=
                        document.documentElement.scrollHeight - 300
                    ) {
                        let loadData = binding.value;
                        loadData();
                    }
                });
            }
        }
    },
    methods: {
        numChange(num, $index, price) {
            this.phoneList[$index].total = (price * num).toFixed(2);
            this.phoneList[$index].num = num;
        },
        init() {
            // this.$http.get('/getCategory').then((res) => {
            //     console.log(res);
            //     this.menuList = res.list;
            // });
            // 查询14条数据，phoneList只用后面8条数据
            this.getProduct();
        },
        async getProduct() {
            if (this.noMore) {
                return false;
            }
            if (!this.loading) {
                this.loading = true;

                let { records, total } = await this.$http.get('/api/home/goods/list', {
                    params: { previewStatus: 0, pageNum: this.pageNum, pageSize: 20 }
                });
                records.forEach((element) => {
                    element.num = 0;
                    element.total = '0.00';
                });

                this.phoneList = this.phoneList.concat(records);
                if (this.phoneList.length >= total) {
                    this.noMore = true;
                } else {
                    this.pageNum++;
                }
                this.loading = false;
            }
        },

        toDetail(id) {
            this.$router.push('/detail/' + id);
        }
    }
};
</script>
<style lang="scss">
@import './../assets/scss/config.scss';
@import './../assets/scss/mixin.scss';
.boxTable {
    padding: 20px 0;
    td {
        padding: 3px 0;
    }
}
.qlist {
    background-color: #f2f2f2;
    min-height: 60vh;
}
.list-boxs {
    padding: 20px 0 60px;

    margin-bottom: 14px;
    width: 100%;
    .list {
        float: left;
        width: 20%;
        box-sizing: border-box;
        height: 312px;
        padding: 7px;
        margin-bottom: 5px;

        .item {
            background-color: $colorG;
            text-align: center;
            height: 100%;
            box-sizing: border-box;
            padding-top: 15px;
            cursor: pointer;

            span {
                display: inline-block;
                width: 67px;
                height: 24px;
                font-size: 14px;
                line-height: 24px;
                color: $colorG;
                &.new-pro {
                    background-color: #7ecf68;
                }
                &.kill-pro {
                    background-color: #e82626;
                }
            }
            .item-img {
                img {
                    width: 100%;
                    height: 195px;
                }
            }
            .item-info {
                h3 {
                    font-size: 14px;
                    color: $colorB;
                    line-height: 14px;
                    font-weight: bold;
                    margin-top: 10px;
                }
                p {
                    color: $colorD;
                    line-height: 13px;
                    margin: 6px auto 13px;
                }
                .price {
                    color: #f20a0a;
                    font-size: $fontJ;
                    font-weight: bold;
                    cursor: pointer;
                    margin-bottom: 0;
                    padding-bottom: 13px;
                    &:after {
                        @include bgImg(22px, 22px, '/imgs/icon-cart-hover.png');
                        content: ' ';
                        margin-left: 5px;
                        vertical-align: middle;
                    }
                }
            }
        }
    }
}
</style>
